package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author lqh
 * @since 2023-09-26
 */
@Api(tags = "优惠卷活动")
@RestController
@RequestMapping("/admin/activity/couponInfo")

public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;

    //1.优惠卷的分页查询接口
    @ApiOperation("优惠卷分页查询接口")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit){
       IPage<CouponInfo> iPage =
               couponInfoService.selectPageCouponInfo(page,limit);
       return Result.ok(iPage);
    }

    //2 添加优惠卷
    @ApiOperation("添加优惠卷")
    @PostMapping("/save")
    public Result save(@RequestBody CouponInfo couponInfo){
        couponInfoService.save(couponInfo);
        return Result.ok(null);
    }

    //3 根据Id查询优惠卷
    @ApiOperation("根据Id查询优惠卷")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        CouponInfo byId = couponInfoService.getCouponInfo(id);
        //有枚举类型 在实现类中 单独封装一下
        return Result.ok(byId);
    }

    //4 根据优惠卷Id查询规则数据
    //url: `${api_name}/findCouponRuleList/${id}`,
    //method: 'get'
    @ApiOperation("根据优惠卷Id查询规则数据")
    @GetMapping("/findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable Long id){
        //类似 在营销活动中 根据Id获得规则一样
        Map<String,Object> map = couponInfoService.findCouponRuleList(id);
        return Result.ok(map);
    }

    //5 添加优惠卷规则数据
    @ApiOperation("添加优惠卷规则数据")
    @PostMapping("/saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo){
        //根据优惠卷Id(coupon_id)先删除  coupon_rang表中的信息
        //在修改基本信息
        //最后在添加
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }

    //修改和删除删除优惠卷
    //修改
    // url: `${api_name}/update`,
    //      method: 'put',
    //      data: role
    @ApiOperation("修改优惠卷列表信息")
    @PutMapping("/update")
    public Result update(@RequestBody CouponInfo couponInfo){
        couponInfoService.updateById(couponInfo);
        return Result.ok(null);
    }

    //removeById (id) {
    //    return request({
    //      url: `${api_name}/remove/${id}`,
    //      method: 'delete'
    //    })
    @ApiOperation("删除规则")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        //删除一个优惠卷活动 连带的活动sku和category都要删除
        couponInfoService.removeAndRang(id);
        return Result.ok(null);
    }

}

